package kr.hkit.androidcomplete02.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01.*;
import android.content.*;
import android.util.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class MyCustomAdapter extends BaseAdapter {

	String TAG = "MyListAdapter";
	LayoutInflater inflate;
	Context mContext;
	ArrayList<MyItem> arLists;
	int layoutRes;
	
	public MyCustomAdapter(Context context, int aResLayout, ArrayList<MyItem> lists) {
		mContext = context;
		arLists = lists;
		layoutRes = aResLayout;
		inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		Log.d(TAG, "getCount()");
		return arLists.size();
	}

	@Override
	public Object getItem(int position) {
		Log.d(TAG, "getItem("+ position +")");
		return arLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		Log.d(TAG, "getItemId("+ position +")");
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d(TAG, "getView("+ position +")");
		final int pos = position;
		if(convertView == null){
			convertView = inflate.inflate(layoutRes, parent, false);
		}
		ImageView img = (ImageView) convertView.findViewById(R.id.ch12_img);
		img.setImageResource(arLists.get(position).iconRes);
		
		TextView tv = (TextView) convertView.findViewById(R.id.ch12_text);
		tv.setText(arLists.get(position).name);
		
		Button btn = (Button) convertView.findViewById(R.id.ch12_btn);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String text = arLists.get(pos).name + "를 주문합니다.";
				Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
			}
		});
		return convertView;
	}

}
